<template>
  <div class="auth-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">{{ isLogin ? 'Sign in' : 'Sign up' }}</h1>
          <p class="text-xs-center">
            
            <nuxt-link v-if="isLogin" to="/register">Need an account?</nuxt-link>
            <nuxt-link v-else to="/login">Have an account?</nuxt-link>
          </p>
          <ul class="error-messages">
            <li>That email is invalid.</li>
          </ul>
          <form @submit.prevent="onStumit">
            <fieldset v-if="!isLogin" class="form-group">
              <input class="form-control form-control-lg" type="text" v-model="user.username" placeholder="Your Name" />
            </fieldset>
            <fieldset class="form-group">
              <input class="form-control form-control-lg" type="email"  v-model="user.email" placeholder="Email" />
            </fieldset>
            <fieldset class="form-group">
              <input class="form-control form-control-lg" type="password" minlength="8" v-model="user.password" placeholder="Password" />
            </fieldset>
            <button
              class="btn btn-lg btn-primary pull-xs-right"
            >{{ isLogin ? 'Sign in' : 'Sign up' }}</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { login, registr } from '@/api/login'

const Cookie = process.client ? require('js-cookie') : null
export default {
  name: "LoginIndex",
  middleware: "notAuthenticated",
  asyncData() {},
  data() {
    return {
      user: {
        username:"",
        email: "",
        password: ""
    }
    }
  },
  computed: {
    isLogin() {
      return this.$route.path === "/login";
    },
  },
  methods:{
   async  onStumit() {
     try {
    const { user }  =   this.isLogin ? await login({user: this.user}) : await registr({user: this.user})
    this.$store.commit('setUser', user)
    Cookie.set('user', user)
      this.$router.push('/')  
     } catch (error) {
       console.log(error)
     }
    }
  }
};
</script>
<style>
</style>